﻿namespace Restaurant
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        private void Enter_Button_Click(object sender, RoutedEventArgs e)
        {
            //UserPassword mainWindow = new UserPassword();
            ////MainWindow mainWindow = new MainWindow();
            //mainWindow.txtID.Text = Password_Box.Password;
            //mainWindow.Show();
            //this.Close();

            string userName = GetNameByID();
            if (userName != "")
            {
                MainWindow mainWindow = new MainWindow();
                mainWindow.UserName.Content = "User: " + userName;
                mainWindow.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("Incorrect ID");
                //LoginWindow loginWindow = new LoginWindow();
                //loginWindow.Show();
                //this.Close();
            }
        }

        private void Exit_Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Password_Box.Password += ((Button)sender).Content.ToString();          
        }

        private void Backspace_Button_Click(object sender, RoutedEventArgs e)
        {
            if (Password_Box.Password.Length > 0)
            {
                int passwordLength = Password_Box.Password.Length;
                Password_Box.Password = Password_Box.Password.Remove(passwordLength - 1);
            }    
        }

        private void Create_Button_Click(object sender, RoutedEventArgs e)
        {
            CreateWindow createWindow = new CreateWindow();
            createWindow.Show();
        }

        private string GetNameByID()
        {
            StringBuilder workersList = FileReader.ReadFile("../../workers.txt", "error.log");
            string[] workers = workersList.ToString().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string name = "";
            foreach (var worker in workers)
            {
                string[] token = worker.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                string num = token[0].Trim();
                if (num == Password_Box.Password)
                {
                    name = token[1].Trim() +"-"+ token[2].Trim(); 
                    return name;
                }
            }
            return "";
        }   
    }
}
